package com.gofaraway.common.domin;

import java.io.Serializable;

/**
 * @author ChenPeng
 * @Date 2024/4/12 15:05
 */
public class Response<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    public static final String ERROR_CODE = "9999";
    public static final String SUCCESS_CODE = "200";
    public static final String SUCCESS_MSG = "success";
    public static final String ERROR_MSG = "error";

    /**
     * 响应码
     */
    private String code;

    /**
     * 响应提示
     */
    private String message;

    /**
     * 返回数据
     */
    private T data;

    /**
     * 是否请求成功
     */
    private boolean success;

    /**
     * 响应正常
     *
     * @param data
     * @param <T>
     * @return
     */
    public static <T> Response<T> success(T data) {
        final Response<T> result = new Response<T>();
        result.setSuccess(true);
        result.setData(data);
        result.setCode(SUCCESS_CODE);
        result.setMessage(SUCCESS_MSG);
        return result;
    }

    /**
     * pageHelper 分页响应
     * @param pageInfo
     * @param <T>
     * @return
     */
    /*public static <T> Response<PagingResponse<T>> success(PageInfo<T> pageInfo) {

        return success(PagingResponse.pageResponse(pageInfo));
    }*/

    /**
     * mybatis-plus 分页响应
     *
     * @param <T>
     * @return
     */
    /*public static <T> Response<PagingResponse<T>> success(IPage<T> iPage) {

        return success(PagingResponse.pageResponse(iPage));
    }*/
    public static <T> Response<T> success() {
        final Response<T> result = new Response<T>();
        result.setSuccess(true);
        result.setData(null);
        result.setCode(SUCCESS_CODE);
        result.setMessage(SUCCESS_MSG);
        return result;
    }


    /**
     * @param code
     * @param message
     * @return
     */
    public static <T> Response<T> error(String code, String message) {
        Response Response = new Response();
        Response.setCode(code);
        Response.setMessage(message);
        Response.setSuccess(false);
        Response.setData(null);
        return Response;
    }

    /**
     * @param message
     * @return
     */
    public static Response errorMsg(String message) {
        return error(ERROR_CODE, message);
    }

    /**
     * @return
     */
    public static Response error() {
        return error(ERROR_CODE, ERROR_MSG);
    }


    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }
}